/**
 * 
 */
package cn.gov.msa.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

 

import cn.gov.msa.collection.LdapService;
import cn.gov.msa.dao.UserDetailMapper;
import cn.gov.msa.model.Org;
import cn.gov.msa.model.Role;
import cn.gov.msa.model.UserDetail;
import cn.gov.msa.model.UserRole;
import cn.gov.msa.service.OrgService;
import cn.gov.msa.service.RoleService;
import cn.gov.msa.service.UserDetailService;
import cn.gov.msa.util.InfoObjUtil;
import cn.gov.msa.util.RedisUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**   
* @Title: UserController.java 
* @Package cn.gov.msa.controller 
* @Description: TODO(用一句话描述该文件做什么) 
* @author 何宇江  
* @date 2018年4月5日 下午8:40:32 
*/

@RestController
@RequestMapping(value = "/user", produces="text/plain;charset=UTF-8")
public class UserController {

	@Autowired
	UserDetailService userDetailService;
	@Autowired
	OrgService orgService;
	
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params,HttpSession session) throws Exception {
		String result = "";
		
		int pageNum = 1;
		int pageSize = 10;
		if (params.has("pageNum")) {
			pageNum = params.getInt("pageNum");
			params.put("currentPage", pageNum);
			params.remove("pageNum");
		} else {
			params.put("currentPage", pageNum);
		}
		if (params.has("pageSize")) {
			pageSize = params.getInt("pageSize");
			// params.remove("pageSize");
		} else {
			params.put("pageSize", pageSize);
		}
		if(!params.has("flag")) {
			Org org = new Org();
			if (!params.has("orgcode")) {
				UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
				org.setOrgCode(userDetail.getOrgcode());
			}else {
				org.setOrgCode(params.getString("orgcode"));
				params.remove("orgcode");
			}
			List<Org> orgs=  orgService.selectOrg(org);
			params.put("orgcode", orgs.get(0).getOrgCode4A());
		}
		System.err.println(params);
		result = LdapService.queryUser(params);

//		UserDetail userDetail = (UserDetail)JSONObject.toBean(params, UserDetail.class);
//		
//		List<UserDetail> userDetailList = userDetailService.selectData(pageNum, pageSize, userDetail);		

	    //获取分页信息
//        PageInfo page = new PageInfo(userDetailList);
        

//		JSONArray ja = JSONArray.fromObject(userDetailList);
//		
//		
//		JSONObject jsonResult = new JSONObject();
//		jsonResult.put("total", page.getTotal());
//		jsonResult.put("rows", ja);
		
		
//		result = jsonResult.toString();
		return result;
	}
	

	@RequestMapping(value = "/rolelist")
	public String roleList(@RequestBody JSONObject params) throws Exception {
		String result = "";

		JSONObject jsonResult = new JSONObject();
		
		String roleCode = params.optString("rolecode");
		if(!"".equals(roleCode)){
			List<UserRole> userRoleList = new ArrayList<UserRole>();
			
			Role role = roleService.getInfoByRoleCode(roleCode);
			if(role.getRoletype().equals("02")){
				JSONArray jaRoleUser = LdapService.getUserByRoleCode(roleCode, null);
				
				for (Object object : jaRoleUser) {
					JSONObject jsonRoleUser = JSONObject.fromObject(object);
					UserRole userRole = new UserRole();
					userRole.setUserid(jsonRoleUser.optString("userid"));
					userRole.setUsername(jsonRoleUser.optString("username"));
					userRole.setOrgcode(jsonRoleUser.optString("orgcode"));
					userRole.setOrgname(jsonRoleUser.optString("orgname"));
					
					userRoleList.add(userRole);					
				}
				System.err.println(">>>>" + jaRoleUser);
			}
			else{
				UserRole userRole = (UserRole)JSONObject.toBean(params, UserRole.class);		
				userRoleList = userDetailMapper.selectUserRole(userRole);	
			}
	
	
			jsonResult.put("total", userRoleList.size());
			jsonResult.put("rows", userRoleList);
		}
		else{
			jsonResult.put("total", 0);
			jsonResult.put("rows", new JSONArray());
		}
		
		
		result = jsonResult.toString();
		return result;
	}
	
	@RequestMapping(value = "/right")
	public String right(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false; 
		
	    try{
		    System.out.println(param);
		    
		    JSONObject jsonUser = param.getJSONObject("user");
		    JSONArray jaUser = param.getJSONArray("role");
		    
		    UserDetail userDetail = (UserDetail)JSONObject.toBean(jsonUser, UserDetail.class);
		    
		    List<Role> roleList = new ArrayList<Role>();
		    for (Object obj : jaUser) {
				JSONObject jsonRole = JSONObject.fromObject(obj);
				Role role = (Role)JSONObject.toBean(jsonRole, Role.class);
				
				roleList.add(role);
			}
		    
		    save = userDetailService.right(userDetail, roleList);

		    if(save){
		    	result = 1;
		    	info = InfoObjUtil.Success_save_OK;

		    	try {
			    	RedisUtils redisUtils = new RedisUtils();
			    	redisUtils.delete("queryLeftMenu*");
				} catch (Exception e) {
					System.err.println("清除缓存失败！UserController  154行");
				}
		    }
		    else{
		    	result = 0;
		    	info = InfoObjUtil.Error_save_no;
		    }
	    }
	    catch(Exception e){
			e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }
	    
	    
	    JSONObject jsonResult = new JSONObject();
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}

	@Autowired
	private RoleService roleService;
	@Autowired
	private UserDetailMapper userDetailMapper;
	
	@RequestMapping(value = "/userrole")
	public String userrole(@RequestBody JSONObject params) throws Exception {
		String result = "";
		
		JSONArray ja = new JSONArray();
		
		List<Role> roleList = roleService.selectAllRole();		
		for (Role role : roleList) {
			JSONObject jsonRole = JSONObject.fromObject(role);
			
			UserRole userRole = new UserRole();
			userRole.setUserid(params.getString("userid"));
			userRole.setRolecode(role.getRolecode());
			List<UserRole> userRoleList = userDetailMapper.selectUserRole(userRole);
			if(userRoleList.size() > 0){
				jsonRole.put("ischeck", true);
			}
			else{
				jsonRole.put("ischeck", false);
			}
			
			ja.add(jsonRole);
		}
		
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", roleList.size());
		jsonResult.put("rows", ja);
		
		
		result = jsonResult.toString();
		return result;
	}
	
	/**
	 * 获取当前登录用户基本信息
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getCurrentUser")
	public String getCurrentUser(HttpSession session) throws Exception {
		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		return JSONObject.fromObject(userDetail).toString();
	}
}
